home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Main.bin / DecimalFormatSymbols.java < prev    next >
Text File  |  1998-09-22  |  12KB  |  390 lines

  1. /*
  2.  * @(#)DecimalFormatSymbols.java    1.17 98/02/19
  3.  *
  4.  * (C) Copyright Taligent, Inc. 1996 - All Rights Reserved
  5.  * (C) Copyright IBM Corp. 1996 - All Rights Reserved
  6.  *
  7.  * Portions copyright (c) 1996-1997 Sun Microsystems, Inc. All Rights Reserved.
  8.  *
  9.  *   The original version of this source code and documentation is copyrighted
  10.  * and owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These
  11.  * materials are provided under terms of a License Agreement between Taligent
  12.  * and Sun. This technology is protected by multiple US and International
  13.  * patents. This notice and attribution to Taligent may not be removed.
  14.  *   Taligent is a registered trademark of Taligent, Inc.
  15.  *
  16.  * Permission to use, copy, modify, and distribute this software
  17.  * and its documentation for NON-COMMERCIAL purposes and without
  18.  * fee is hereby granted provided that this copyright notice
  19.  * appears in all copies. Please refer to the file "copyright.html"
  20.  * for further important copyright and licensing information.
  21.  *
  22.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  23.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  24.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  25.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  26.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  27.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  28.  *
  29.  */
  30.  
  31. package java.text;
  32. import java.io.IOException;
  33. import java.io.ObjectInputStream;
  34. import java.io.Serializable;
  35. import java.util.ResourceBundle;
  36. import java.util.Locale;
  37. import java.util.Hashtable;
  38.  
  39. /**
  40.  * This class represents the set of symbols (such as the decimal separator,
  41.  * the grouping separator, and so on) needed by <code>DecimalFormat</code>
  42.  * to format numbers. <code>DecimalFormat</code> creates for itself an instance of
  43.  * <code>DecimalFormatSymbols</code> from its locale data.  If you need to change any
  44.  * of these symbols, you can get the <code>DecimalFormatSymbols</code> object from
  45.  * your <code>DecimalFormat</code> and modify it.
  46.  *
  47.  * @see          java.util.Locale
  48.  * @see          DecimalFormat
  49.  * @version      1.12 29 Jan 1997
  50.  * @author       Mark Davis
  51.  * @author       Alan Liu
  52.  */
  53.  
  54. final public class DecimalFormatSymbols implements Cloneable, Serializable {
  55.  
  56.     /**
  57.      * Create a DecimalFormatSymbols object for the default locale.
  58.      */
  59.     public DecimalFormatSymbols() {
  60.         initialize( Locale.getDefault() );
  61.     }
  62.  
  63.     /**
  64.      * Create a DecimalFormatSymbols object for the given locale.
  65.      */
  66.     public DecimalFormatSymbols( Locale locale ) {
  67.         initialize( locale );
  68.     }
  69.  
  70.     /**
  71.      * character used for zero. Different for Arabic, etc.
  72.      */
  73.     public char getZeroDigit() {
  74.         return zeroDigit;
  75.     }
  76.  
  77.     public void setZeroDigit(char zeroDigit) {
  78.         this.zeroDigit = zeroDigit;
  79.     }
  80.  
  81.     /**
  82.      * character used for thousands separator. Different for French, etc.
  83.      */
  84.     public char getGroupingSeparator() {
  85.         return groupingSeparator;
  86.     }
  87.  
  88.     public void setGroupingSeparator(char groupingSeparator) {
  89.         this.groupingSeparator = groupingSeparator;
  90.     }
  91.  
  92.     /**
  93.      * character used for decimal sign. Different for French, etc.
  94.      */
  95.     public char getDecimalSeparator() {
  96.         return decimalSeparator;
  97.     }
  98.  
  99.     public void setDecimalSeparator(char decimalSeparator) {
  100.         this.decimalSeparator = decimalSeparator;
  101.     }
  102.  
  103.     /**
  104.      * character used for mille percent sign. Different for Arabic, etc.
  105.      */
  106.     public char getPerMill() {
  107.         return perMill;
  108.     }
  109.  
  110.     public void setPerMill(char perMill) {
  111.         this.perMill = perMill;
  112.     }
  113.  
  114.     /**
  115.      * character used for percent sign. Different for Arabic, etc.
  116.      */
  117.     public char getPercent() {
  118.         return percent;
  119.     }
  120.  
  121.     public void setPercent(char percent) {
  122.         this.percent = percent;
  123.     }
  124.  
  125.     /**
  126.      * character used for a digit in a pattern.
  127.      */
  128.     public char getDigit() {
  129.         return digit;
  130.     }
  131.  
  132.     public void setDigit(char digit) {
  133.         this.digit = digit;
  134.     }
  135.  
  136.     /**
  137.      * character used to separate positive and negative subpatterns
  138.      * in a pattern.
  139.      */
  140.     public char getPatternSeparator() {
  141.         return patternSeparator;
  142.     }
  143.  
  144.     public void setPatternSeparator(char patternSeparator) {
  145.         this.patternSeparator = patternSeparator;
  146.     }
  147.  
  148.     /**
  149.      * character used to represent infinity. Almost always left
  150.      * unchanged.
  151.      */
  152.  
  153.     public String getInfinity() {
  154.         return infinity;
  155.     }
  156.  
  157.     public void setInfinity(String infinity) {
  158.         this.infinity = infinity;
  159.     }
  160.  
  161.     /**
  162.      * character used to represent NaN. Almost always left
  163.      * unchanged.
  164.      */
  165.     public String getNaN() {
  166.         return NaN;
  167.     }
  168.  
  169.     public void setNaN(String NaN) {
  170.         this.NaN = NaN;
  171.     }
  172.  
  173.     /**
  174.      * character used to represent minus sign. If no explicit
  175.      * negative format is specified, one is formed by prefixing
  176.      * minusSign to the positive format.
  177.      */
  178.     public char getMinusSign() {
  179.         return minusSign;
  180.     }
  181.  
  182.     public void setMinusSign(char minusSign) {
  183.         this.minusSign = minusSign;
  184.     }
  185.  
  186.     //------------------------------------------------------------
  187.     // BEGIN   Package Private methods ... to be made public later
  188.     //------------------------------------------------------------
  189.  
  190.     /**
  191.      * Return the character used to separate the mantissa from the exponent.
  192.      */
  193.     char getExponentialSymbol()
  194.     {
  195.     return exponential;
  196.     }
  197.  
  198.     /**
  199.      * Set the character used to separate the mantissa from the exponent.
  200.      */
  201.     void setExponentialSymbol(char exp)
  202.     {
  203.     exponential = exp;
  204.     }
  205.  
  206.     /**
  207.      * Return the string denoting the local currency.
  208.      */
  209.     String getCurrencySymbol()
  210.     {
  211.     return currencySymbol;
  212.     }
  213.  
  214.     /**
  215.      * Set the string denoting the local currency.
  216.      */
  217.     void setCurrencySymbol(String currency)
  218.     {
  219.     currencySymbol = currency;
  220.     }
  221.  
  222.     /**
  223.      * Return the international string denoting the local currency.
  224.      */
  225.     String getInternationalCurrencySymbol()
  226.     {
  227.     return intlCurrencySymbol;
  228.     }
  229.  
  230.     /**
  231.      * Set the international string denoting the local currency.
  232.      */
  233.     void setInternationalCurrencySymbol(String currency)
  234.     {
  235.     intlCurrencySymbol = currency;
  236.     }
  237.  
  238.     /**
  239.      * Return the monetary decimal separator.
  240.      */
  241.     char getMonetaryDecimalSeparator()
  242.     {
  243.     return monetarySeparator;
  244.     }
  245.  
  246.     /**
  247.      * Set the monetary decimal separator.
  248.      */
  249.     void setMonetaryDecimalSeparator(char sep)
  250.     {
  251.     monetarySeparator = sep;
  252.     }
  253.  
  254.     //------------------------------------------------------------
  255.     // END     Package Private methods ... to be made public later
  256.     //------------------------------------------------------------
  257.  
  258.     /**
  259.      * Standard override.
  260.      */
  261.     public Object clone() {
  262.         try {
  263.             return (DecimalFormatSymbols)super.clone();
  264.             // other fields are bit-copied
  265.         } catch (CloneNotSupportedException e) {
  266.             throw new InternalError();
  267.         }
  268.     }
  269.  
  270.     /**
  271.      * Override equals
  272.      */
  273.     public boolean equals(Object obj) {
  274.         if (obj == null) return false;
  275.         if (this == obj) return true;
  276.         if (getClass() != obj.getClass()) return false;
  277.         DecimalFormatSymbols other = (DecimalFormatSymbols) obj;
  278.         return (zeroDigit == other.zeroDigit &&
  279.         groupingSeparator == other.groupingSeparator &&
  280.         decimalSeparator == other.decimalSeparator &&
  281.         percent == other.percent &&
  282.         perMill == other.perMill &&
  283.         digit == other.digit &&
  284.         minusSign == other.minusSign &&
  285.         patternSeparator == other.patternSeparator &&
  286.         infinity.equals(other.infinity) &&
  287.         NaN.equals(other.NaN) &&
  288.         currencySymbol.equals(other.currencySymbol) &&
  289.         intlCurrencySymbol.equals(other.intlCurrencySymbol) &&
  290.         monetarySeparator == other.monetarySeparator);
  291.     }
  292.  
  293.     /**
  294.      * Override hashCode
  295.      */
  296.     public int hashCode() {
  297.             int result = zeroDigit;
  298.             result = result * 37 + groupingSeparator;
  299.             result = result * 37 + decimalSeparator;
  300.             return result;
  301.     }
  302.  
  303.     /**
  304.      * Initializes the symbols from the LocaleElements resource bundle.
  305.      * Note: The organization of LocaleElements badly needs to be
  306.      * cleaned up.
  307.      */
  308.     private void initialize( Locale locale ) {
  309.     /* try the cache first */
  310.     String[][] data = (String[][]) cachedLocaleData.get(locale);
  311.     String[] numberElements;
  312.     String[] currencyElements;
  313.     if (data == null) {  /* cache miss */
  314.         data = new String[2][];
  315.         ResourceBundle rb = ResourceBundle.getBundle
  316.         ("java.text.resources.LocaleElements", locale);
  317.         data[0] = rb.getStringArray("NumberElements");
  318.         data[1] = rb.getStringArray("CurrencyElements");
  319.         /* update cache */
  320.         cachedLocaleData.put(locale, data);
  321.     }
  322.     numberElements = data[0];
  323.     currencyElements = data[1];
  324.     
  325.         decimalSeparator = numberElements[0].charAt(0);
  326.         groupingSeparator = numberElements[1].charAt(0);
  327.         patternSeparator = numberElements[2].charAt(0);
  328.         percent = numberElements[3].charAt(0);
  329.         zeroDigit = numberElements[4].charAt(0); //different for Arabic,etc.
  330.         digit = numberElements[5].charAt(0);
  331.         minusSign = numberElements[6].charAt(0);
  332.     exponential = numberElements[7].charAt(0);
  333.         perMill = numberElements[8].charAt(0);
  334.         infinity  = numberElements[9];
  335.         NaN = numberElements[10];
  336.  
  337.     currencySymbol = currencyElements[0];
  338.         intlCurrencySymbol = currencyElements[1];
  339.     monetarySeparator = currencyElements[2].charAt(0);
  340.     }
  341.  
  342.     /**
  343.      * Override readObject.
  344.      */
  345.     private void readObject(ObjectInputStream stream)
  346.      throws IOException, ClassNotFoundException
  347.     {
  348.     stream.defaultReadObject();
  349.     if (serialVersionOnStream < 1)
  350.     {
  351.         // Didn't have monetarySeparator or exponential field;
  352.         // use defaults.
  353.         monetarySeparator = decimalSeparator;
  354.         exponential       = 'E';
  355.     }
  356.     serialVersionOnStream = currentSerialVersion;
  357.     }
  358.  
  359.     private  char    zeroDigit;
  360.     private  char    groupingSeparator;
  361.     private  char    decimalSeparator;
  362.     private  char    perMill;
  363.     private  char    percent;
  364.     private  char    digit;
  365.     private  char    patternSeparator;
  366.     private  String  infinity;
  367.     private  String  NaN;
  368.     private  char    minusSign;
  369.     private  String  currencySymbol;
  370.     private  String  intlCurrencySymbol;
  371.     private  char    monetarySeparator; // Field new in JDK 1.1.6
  372.     private  char    exponential;       // Field new in JDK 1.1.6
  373.  
  374.     // Proclaim JDK 1.1 FCS compatibility
  375.     static final long serialVersionUID = 5772796243397350300L;
  376.  
  377.     // The internal serial version which says which version was written
  378.     // - 0 (default) for version up to JDK 1.1.5
  379.     // - 1 for version from JDK 1.1.6, which includes two new fields:
  380.     //     monetarySeparator and exponential.
  381.     private static final int currentSerialVersion = 1;
  382.     private int serialVersionOnStream = currentSerialVersion;
  383.  
  384.     /**
  385.      * cache to hold the NumberElements and the CurrencyElements
  386.      * of a Locale.
  387.      */
  388.     private static final Hashtable cachedLocaleData = new Hashtable(3);
  389. }
  390.